home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / exarray.com / X_TABLE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-08-19  |  3.2 KB  |  151 lines

  1. Unit X_Table;
  2. {$R-,S-,O+}
  3.  
  4. { The ExtendedTable provides the ExtendedArray with access to all Lobes, }
  5. { either directly, as in the case of a Lobe which currently resides in   }
  6. { one of the (MaxBuff+1) array locations, or by swapping one of these to }
  7. { disk and swapping in the requested Lobe.                               }
  8.  
  9. INTERFACE
  10.  
  11. Uses XManager,XGlobals,ExtBuff,FlexPntr;
  12.  
  13. Type
  14.   BufferWindow = Array[0..MaxBuff] of ExtendedBuffer;
  15.   Priority     = Array[0..MaxBuff] of LongInt;
  16.  
  17.   ExtendedTable = Object
  18.  
  19.                    Buff : BufferWindow;
  20.                    Pty  : Priority;
  21.                    Keys : Manager;
  22.                    Ex   : Ext;
  23.  
  24.                    Procedure Create;
  25.                    Procedure Init (NumLobes : Word; LobeSize : Word);
  26.  
  27.                    Function Retrieve (Lobe : Word) : FlexPtr;
  28.  
  29.                    Procedure Update (Lobe : Word; Field : FlexPtr);
  30.  
  31.                    Procedure Destroy;
  32.                  End;
  33.  
  34. IMPLEMENTATION
  35.  
  36. Procedure ExtendedTable.Create;
  37. Var
  38.   I : Byte;
  39. Begin
  40.   For I := 0 to MaxBuff do
  41.     Begin
  42.       Pty[I] := 0;
  43.       Buff[I].Create
  44.     End;
  45.   Keys.Create;
  46.   Ex := ''
  47. End;
  48.  
  49. Procedure ExtendedTable.Destroy;
  50. Var
  51.   I : Byte;
  52.   J : Word;
  53.   F : File;
  54. Begin
  55.   For I := 0 to MaxBuff do
  56.     Begin
  57.       Pty[I] := 0;
  58.       Buff[I].Destroy
  59.     End;
  60.   For J := 0 to Keys.MaxSize-1 do
  61.     Begin
  62.       {$I-}
  63.       Assign (F,Keys.GetTag (J)+Ex);
  64.       Reset (F);
  65.       {$I+}
  66.       If IOResult = 0 Then
  67.         Begin
  68.           Close (F);
  69.           Erase (F)
  70.         End
  71.     End;
  72.   Keys.Destroy;
  73.   Ex := ''
  74. End;
  75.  
  76. Procedure ExtendedTable.Init (NumLobes : Word; LobeSize : Word);
  77. Var
  78.   I : Word;
  79.   J : Byte;
  80. Begin
  81.   Ex := Generated_Extension;
  82.   Keys.Init (NumLobes);
  83.   For I := 0 to NumLobes-1 do Keys.SetTag (I,'}'+Int_To_Short (I)+ '{');
  84.  
  85.   For J := 0 to MaxBuff do
  86.     Begin
  87.       Buff[J].Init (Keys.GetTag(J),Ex,LobeSize);
  88.       Pty[J] := 1;
  89.       Keys.Load (J,J)
  90.     End
  91. End;
  92.  
  93. Function LeastPty (P : Priority) : Byte;
  94.  
  95. { Provide the Index of the Buffer with the least priority }
  96.  
  97. Var
  98.   I,K : Byte;
  99.   J   : LongInt;
  100. Begin
  101.   J := P[0];
  102.   K := 0;
  103.   For I := 1 to MaxBuff do
  104.     If P[I] < J Then
  105.       Begin
  106.         J := P[I];
  107.         K := I
  108.       End;
  109.   LeastPty := K;
  110. End;
  111.  
  112. Function ExtendedTable.Retrieve (Lobe : Word) : FlexPtr;
  113. Var
  114.   K : Byte;
  115. Begin
  116.   If Keys.InMem (Lobe) Then   {If its already in memory, simply provide it}
  117.     K := Keys.MemIndex (Lobe)
  118.   Else
  119.     Begin {Find InMem Lobe with Least Priority and swap it out}
  120.       K := LeastPty (Pty);  {InMem address of Lobe with Least Priority}
  121.  
  122.       Keys.UnLoad (Keys.ManIndex (Buff[K].Tag));
  123.       Keys.Load (Lobe,K);
  124.       Buff[K].SwapData (Keys.GetTag (Lobe));
  125.  
  126.       Pty[K] := Keys.PriIndex (K)
  127.     End;
  128.   Retrieve := Buff[K].Data
  129. End;
  130.  
  131. Procedure ExtendedTable.Update (Lobe : Word; Field : FlexPtr);
  132. Var
  133.   K : Byte;
  134. Begin
  135.   If Keys.InMem (Lobe) Then
  136.     K := Keys.MemIndex (Lobe)
  137.   Else
  138.     Begin
  139.       K := LeastPty (Pty);
  140.  
  141.       Keys.UnLoad (Keys.ManIndex (Buff[K].Tag));
  142.       Keys.Load (Lobe,K);
  143.       Buff[K].SwapData (Keys.GetTag (Lobe));
  144.  
  145.       Pty[K] := Keys.PriIndex (K)
  146.     End;
  147.   Buff[K].Accept (Field)
  148. End;
  149.  
  150. BEGIN
  151. END.